package com.burgeon.backend.SHOPBACKEND.rest;

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.Dimdef;
import com.burgeon.backend.SHOPBACKEND.entity.Order;
import com.burgeon.backend.SHOPBACKEND.entity.ShopOrder;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class QuerySOrderRetailsCode extends RESTBase {
    private String ip;
    private String account;
    private String password;

    public QuerySOrderRetailsCode (String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     * @return
     */

    public ArrayList<Order> getData(String docno) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(docno), url);

        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<Order> orderArrayList = new ArrayList<Order>();
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONArray array = jsonArray.getJSONArray(i);

            Order order = new Order();
            order.setDocno(docno);
            order.setId(jsonArray.getJSONArray(i).getString(0));
            order.setBilldate(jsonArray.getJSONArray(i).getString(1));
            order.setXsno(jsonArray.getJSONArray(i).getString(2));
            order.setStatus(jsonArray.getJSONArray(i).getString(3));
            order.setDisStatus(jsonArray.getJSONArray(i).getString(4));//YC_RETAIL_ID
            order.setIsUp(jsonArray.getJSONArray(i).getString(5));
            //order.setYcRetailId(jsonArray.getJSONArray(i).getString(5));


            orderArrayList.add(order);
        }
        return orderArrayList;
    }

    private MultiValueMap Data(String docno) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");
        columns.add("BILLDATE");//单据日期
        columns.add("XSNO");//线上单号
        columns.add("STATUS");//提交状态
        columns.add("DIS_STATUS");//分单状态
        columns.add("IS_UP");//提交状态


        params.put("columns", columns);
        params.put("table", "YC_RETAIL");//表名

        params.put("count","true");

        HashMap paramss = new HashMap();//DOCNO  (AK)	单据编号
        String ac = "'="+docno+"'";
        paramss.put("column", "'DOCNO'");
        paramss.put("condition", ac);
        params.put("params", paramss);

        return DATA("Query", params,account,password);
    }

    public static void main(String[] args) {
        QuerySOrderRetailsCode querySOrderRetailsCode = new QuerySOrderRetailsCode("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(querySOrderRetailsCode.getData("YCRE21032100005"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}



